﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnetaMvc.ApplicationServices;
using EnetaMvc.ApplicationServices.Dto;
using EnetaMvc.Web.Attributes;
using EnetaMvc.Web.Models;

namespace EnetaMvc.Web.Controllers
{
    public class TagGroupsController : BaseController
    {
        private readonly IContentService _contentService;

        public TagGroupsController(IContentService contentService)
        {
            _contentService = contentService;
        }

        [EnetaAuthorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            TagGroupModel model = new TagGroupModel();
            model.TagGroups = _contentService.ListTagGroups();
            model.TagGroup = new TagGroupDto();

            return View(model);
        }

        [EnetaAuthorize(Roles = "Administrator")]
        [HttpPost]
        public ActionResult Create(TagGroupDto tagGroup)
        {
            if (!ModelState.IsValid)
                return View(tagGroup);
            
            _contentService.SaveTagGroup(tagGroup);
            return RedirectToAction("Index");
        }

        [EnetaAuthorize(Roles = "Administrator")]
        [HttpPost]
        public ActionResult Edit(TagGroupDto tagGroup)
        {
            if (!ModelState.IsValid)
                return View(tagGroup);

            _contentService.SaveTagGroup(tagGroup);
            return RedirectToAction("Index");
        }

        [EnetaAuthorize(Roles = "Administrator")]
        [HttpPost]
        public ActionResult Delete(int id)
        {
            _contentService.DeleteTagGroup(id);
            return RedirectToAction("Index");
        }

        [EnetaAuthorize(Roles = "Administrator")]
        public JsonResult HasTags(int id)
        {
            return Json(_contentService.HasTags(id), JsonRequestBehavior.AllowGet);
        }
    }
}
